package com.zerui.manager.accessauthpersonnel.gatewayimpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zerui.manager.accessauthpersonnel.AccessAuthPersonnel;
import com.zerui.manager.accessauthpersonnel.convertor.AccessAuthPersonnelConvert;
import com.zerui.manager.accessauthpersonnel.entity.AccessAuthPersonnelDo;
import com.zerui.manager.accessauthpersonnel.gateway.AccessAuthPersonnelGateWay;
import com.zerui.manager.accessauthpersonnel.mapper.AccessAuthPersonnelMapper;
import com.zerui.manager.web.cmd.AccessAuthPersonnelBindCmd;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class AccessAuthPersonnelGateWayImpl implements AccessAuthPersonnelGateWay {

    @Resource
    private AccessAuthPersonnelMapper accessAuthPersonnelMapper;

    @Override
    public List<AccessAuthPersonnel> getByPersonnelIds(List<Long> personnelIds) {
        LambdaQueryWrapper<AccessAuthPersonnelDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AccessAuthPersonnelDo::getPersonnelId,personnelIds);
        List<AccessAuthPersonnelDo> accessAuthPersonnelDos = accessAuthPersonnelMapper.selectList(queryWrapper);
        return AccessAuthPersonnelConvert.INSTANCE.covertPersonnelDoToAccessAuthPersonnel(accessAuthPersonnelDos);
    }

    @Override
    public void addBatch(List<AccessAuthPersonnel> accessAuthPersonals) {
        List<AccessAuthPersonnelDo> accessAuthPersonnelDos = AccessAuthPersonnelConvert.INSTANCE.covertPersonnelToAccessAuthPersonnelDo(accessAuthPersonals);
        accessAuthPersonnelMapper.insertBatchSomeColumn(accessAuthPersonnelDos);
    }

    @Override
    public List<Long> deleteBatch(AccessAuthPersonnelBindCmd cmd) {
        LambdaQueryWrapper<AccessAuthPersonnelDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthPersonnelDo::getAccessAuthId,cmd.getAccessAuthId());
        queryWrapper.in(AccessAuthPersonnelDo::getPersonnelId,cmd.getPersonnelIds());
        List<AccessAuthPersonnelDo> accessAuthPersonnelDos = accessAuthPersonnelMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(accessAuthPersonnelDos)){
            accessAuthPersonnelMapper.delete(queryWrapper);
            return accessAuthPersonnelDos.stream().map(AccessAuthPersonnelDo::getPersonnelId).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<Long> deleteAccessAuthPersonnelById(Long id) {
        LambdaQueryWrapper<AccessAuthPersonnelDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthPersonnelDo::getAccessAuthId,id);
        List<AccessAuthPersonnelDo> personnelDos = accessAuthPersonnelMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(personnelDos)){
            accessAuthPersonnelMapper.delete(queryWrapper);
            return personnelDos.stream()
                    .map(AccessAuthPersonnelDo::getPersonnelId)
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<Long> getAccessAuthPersonnelById(Long accessAuthId) {
        LambdaQueryWrapper<AccessAuthPersonnelDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthPersonnelDo::getAccessAuthId,accessAuthId);
        List<AccessAuthPersonnelDo> personnelDos = accessAuthPersonnelMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(personnelDos)){
            return personnelDos.stream()
                    .map(AccessAuthPersonnelDo::getPersonnelId)
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public Set<Long> filterAccessAuthPersonnelByIds(Long accessAuthId, Set<Long> finalPersonnelList) {
        LambdaQueryWrapper<AccessAuthPersonnelDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AccessAuthPersonnelDo::getPersonnelId,finalPersonnelList);
        queryWrapper.ne(AccessAuthPersonnelDo::getAccessAuthId,accessAuthId);
        List<AccessAuthPersonnelDo> personnelDos = accessAuthPersonnelMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(personnelDos)){
            Set<Long> redundantSet = personnelDos.stream().map(AccessAuthPersonnelDo::getPersonnelId).collect(Collectors.toSet());
            finalPersonnelList.removeAll(redundantSet);
        }
        return finalPersonnelList;
    }

    @Override
    public void deleteById(Long id) {
        accessAuthPersonnelMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> accessAuthPersonIds) {
        accessAuthPersonnelMapper.deleteBatchIds(accessAuthPersonIds);
    }

}
